﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Core.Persistent;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Core.MyLinq;
using Huirui.Cavan.Core.Extension;

namespace Huirui.Cavan.Domain.MainBoundedContext.StockModule
{
    public class StockInOutSpecification : ISpecification<StockInOut, StockInOutSearch>
    {
        #region ISpecification<StockIn,StockInSearch> Members

        public StockInOutSearch SearchParams
        {
            get;
            set;
        }

        #endregion

        #region ISpecification<StockIn> Members

        public System.Linq.Expressions.Expression<Func<StockInOut, bool>> Expression
        {
            get
            {
                IQueryBuilder<StockInOut> queryBuilder = QueryBuilder.Create<StockInOut>();

                if (SearchParams.IsNotNull())
                {
                    if (SearchParams.PurchaseOrderCode.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(si => si.SourceCode, SearchParams.PurchaseOrderCode);
                    }
                    if (SearchParams.StartDate.HasValue && SearchParams.EndDate.HasValue)
                    {
                        queryBuilder = queryBuilder.Greater(si => si.InOutDate, SearchParams.StartDate.Value);
                        queryBuilder = queryBuilder.LessThanEqual(si => si.InOutDate, SearchParams.EndDate.Value);
                    }
                    if (SearchParams.SupplierCode.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(si => si.Supplier.Id, SearchParams.SupplierCode);
                    }
                    if (SearchParams.ID.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(si => si.Id, SearchParams.ID);
                    }
                }

                return queryBuilder.Expression.Expand();
            }
        }

        #endregion
    }
}
